package gms.dao.field;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import gms.entities.field.Field;
import gms.entities.field.Field_rental;
import gms.entities.field.Rent;

public class FieldDao extends HibernateDaoSupport {

	
	//����ID���ҳ��أ�����һ�����ض���
	public Field findFieldById(int id){
		return this.getHibernateTemplate().get(Field.class, id);
	}
	//�����͹ؼ��ֵĲ���
	public List<Field> searchByType(String fieldType){
		return (List<Field>) this.getHibernateTemplate().find("from Field f where f.field_type = ?", fieldType);
	}

	//查找所有场地信息
	public List<Field> findAllField(){
		return (List<Field>) this.getHibernateTemplate().find("from Field f");
	}
	
	//查找用户自己租用的场地和未被租用的场地
	public List<Field> findUserField(String username){
		List<Field> list = (List<Field>) this.getHibernateTemplate().find("from Field f where f.field_mark=?",0);
		List<Rent> list_rent = findRentByUsername(username);
		for(Rent r:list_rent){
			list.add(findFieldById((r.getFacilityId())));
		}
		return list;
	}
	
	//����һ�����ض���
	public void updateField(Field f){
		this.getHibernateTemplate().update(f);
	}
	
	//���泡����Ϣ
	public void saveField(Field f){
		this.getHibernateTemplate().save(f);
	}
	
	//ɾ��һ����¼
	public void deleteField(Field f){
		this.getHibernateTemplate().delete(f);
	}
	
	//��������Ϣ ��id����
	public List<Field_rental> rentalRecordById(int id){
		return (List<Field_rental>) this.getHibernateTemplate()
				.find("from Field_rental fr where fr.field_rental_id=?", id);
	}
	public List<Field_rental> findAllFieldRental(){
		// TODO Auto-generated method stub
		return (List<Field_rental>) this.getHibernateTemplate().find("from Field_rental fr");
	}
	//更新租金表
	public void updateFieldRental(Field_rental fr){
		this.getHibernateTemplate().update(fr);
	}
	
	//保存rent表
	public void saveRent(Rent rent){
		this.getHibernateTemplate().save(rent);
	}
	public void deleteRentById(int facillityId) {
		// TODO Auto-generated method stub
		List<Rent> list = (List<Rent>) this.getHibernateTemplate().find("from Rent r where r.facilityId=?", facillityId);
		Rent rent = list.get(0);
		this.getHibernateTemplate().delete(rent);
	}
	public List<Rent> findRentByUsername(String username) {
		// TODO Auto-generated method stub
		List<Rent> list = (List<Rent>) this.getHibernateTemplate().find("from Rent r where r.username=?", username);
		return list;
	}
}
